let utils = {};
const fs = require('fs');
const qr = require('qr-image');
const xml2js = require('xml2js');

const crypto = require('crypto');
const aes_algorithm = 'aes-128-ecb';
const aes_secrect = '3TRda8dbcUK75S';

utils.encrypt = function(text) {
    let cipher = crypto.createCipher(aes_algorithm, aes_secrect);
    let crypted = cipher.update(text, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted;
};

utils.decrypt = function(text) {
    let decipher = crypto.createDecipher(aes_algorithm, aes_secrect);
    let dec = decipher.update(text, 'hex', 'utf8');
    dec += decipher.final('utf8');
    return dec;
};
/**
 * 根据地址生成二维码
 * 参数 url(string) 地址
 * 参数 callback(Function)
 */
utils.createQr = function(url) {
    return new Promise(resolve => {
        let qr_png = qr.image(url, {type: 'png', size: 6});
        let imgName = +(new Date()) + '' + Math.ceil(Math.random() * 89 + 10);
        imgName = `${imgName}.png`;
        let qr_pipe = qr_png.pipe(fs.createWriteStream('./www/static/code/' + imgName));
        qr_pipe.on('error', function(err) {
            return;
        });
        qr_pipe.on('finish', function() {
            resolve('http://mapi.cz888.com/static/code/' + imgName);
        });
    });
};
//分销二维码
utils.createDisQr = function(uuid, url) {
    return new Promise(resolve => {
        let qr_png = qr.image(url, {type: 'png', size: 6});
        let imgName = +(new Date()) + '' + Math.ceil(Math.random() * 89 + 10);
        imgName = `${uuid}.png`;
        let qr_pipe = qr_png.pipe(fs.createWriteStream('D:/static/code/' + imgName));
        qr_pipe.on('error', function(err) {
            return;
        });
        qr_pipe.on('finish', function() {
            resolve('https://image.guochawenhua.com/code/' + imgName);
        });
    });
};
utils.createQrUrl = function(url) {
    return new Promise(resolve => {
        let qr_png = qr.image(url, {type: 'png', size: 6});
        let imgName = +(new Date()) + '' + Math.ceil(Math.random() * 89 + 10);
        imgName = `${imgName}.png`;
        let qr_pipe = qr_png.pipe(fs.createWriteStream('D:/static/head/' + imgName));
        qr_pipe.on('error', function(err) {
            return;
        });
        qr_pipe.on('finish', function() {
            resolve('https://image.guochawenhua.com/head/' + imgName);
        });
    });
};
utils.getText = function(str) {
    let reg = new RegExp('[\u4e00-\u9fa5]+$', 'g');
    str = str.match(/[\u4e00-\u9fa5]/g);
    let strs = str == null ? '' : str.join('');
    return strs;
};
utils.getImgSrc = function(htmlstr, n) {
    let reg = /<img.+?src=('|")?([^'"]+)('|")?(?:\s+|>)/gim;
    let arr = [];
    while (tem = reg.exec(htmlstr)) {
        arr.push(tem[2]);
    }
    return arr.splice(0, n);
};
utils.buildXML = function(json) {
    var builder = new xml2js.Builder();
    return builder.buildObject(json);
};

utils.parseXML = function(xml, fn) {
    var parser = new xml2js.Parser({trim: true, explicitArray: false, explicitRoot: false});
    parser.parseString(xml, fn || function(err, result) {
    });
};
utils.sortDict = function(dict) {
    let dict2 = {},
        keys = Object.keys(dict).sort();
    for (let i = 0, n = keys.length, key; i < n; ++i) {
        key = keys[i];
        dict2[key] = dict[key];
    }
    return dict2;
};
utils.md5 = function(data) {
    return crypto.createHash('md5').update(data).digest('hex');
};
utils.IsPhone = function(Phone) {
    // return /^((13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8})|(100(09|32)\d{6})$/.test(Phone);
    return /^((13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8})$/.test(Phone);
};
utils.random = function() {
    let result = [];
    for (let i = 0; i < 2; i++) {
        let ranNum = Math.ceil(Math.random() * 25); //生成一个0到25的数字
        result.push(String.fromCharCode(65 + ranNum));
    }
    let code = Math.random().toString().substr(2, 6);
    return result.join('') + code;
};
module.exports = utils;
